package com.zhuss.product.processor;

import java.util.Date;

import com.zhuss.enums.OperationStatus;
import com.zhuss.product.db.dao.ext.LineStrategyDao;
import com.zhuss.product.db.entity.LineStrategy;
import com.zhuss.product.processor.cache.LineStrategyCache;
import com.zhuss.product.processor.cache.SiteStrategyCache;
import com.zhuss.product.processor.cache.StrategyTrainCache;
import com.zhuss.product.processor.cache.TrainCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.train.StartTrainBo;
import com.zhuss.bean.vo.product.train.DetailTrainVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.product.db.dao.ext.TrainDao;
import com.zhuss.product.db.entity.Train;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * 创建车次 processor
 * @Author: zhushanshuo
 * @Date: Tue Jan 12 08:53:55 CST 2021
 */
@Component("trainStart")
public class TrainStart extends AbstractProcessor<Object, StartTrainBo> implements Processor<Object, StartTrainBo> {

    @Autowired
    private TrainDao trainDao;

    @Autowired
	@Qualifier("trainRead")
	private TrainRead trainRead;

    @Autowired
    private TrainCheck trainCheck;

    @Autowired
	@Qualifier("trainCache")
    private TrainCache trainCache;

    @Autowired
    private LineStrategyDao lineStrategyDao;

	@Autowired
	@Qualifier("lineStrategyCache")
    private LineStrategyCache lineStrategyCache;

	@Autowired
	@Qualifier("strategyTrainCache")
	private StrategyTrainCache strategyTrainCache;

	@Autowired
	@Qualifier("siteStrategyCache")
	private SiteStrategyCache siteStrategyCache;




    /**
     * 校验
     */
    @Override
    public void check(StartTrainBo bo) throws DzcxException {
		if (bo.getTrainId() ==null || bo.getTrainId() <=0l) {
			throw new DzcxException(ExceptionCode.车次id为空);
		}
		bo.setStatus(OperationStatus.已开通.key());
    	// 当前车次不满足启用条件
		if (!trainCheck.checkStart(bo.getTrainId())) {
			throw new DzcxException(ExceptionCode.当前车次不满足运营条件);
		}
    }

    /**
     * 操作方法
     * @throws DzcxException
     */
    @Override
    public Object process(StartTrainBo param) throws DzcxException {

    	Long trainId = param.getTrainId();

    	Train record = new Train();
    	record.setTrainId(param.getTrainId());
    	record.setStatus(OperationStatus.已开通.key());

    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	trainDao.updateByPrimaryKeySelective(record);

		DetailTrainVo vo = trainRead.detail(trainId);

    	param.setName(vo.getName());

		trainCache.clear(trainId);

		//更新策略状态
		LineStrategy strategy = lineStrategyDao.selectByPrimaryKey(vo.getLineStrategyId());
		if (!strategy.getStatus().equals(OperationStatus.已开通.key()))  {
			strategy.setStatus(OperationStatus.已开通.key());
			lineStrategyDao.updateByPrimaryKey(strategy);
			//清除线路策略缓存
			lineStrategyCache.clear(strategy.getLineStrategyId());
			siteStrategyCache.clealAll();
		}
		//清除线路策略的日期车次缓存
		strategyTrainCache.clear(strategy.getLineStrategyId());

        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(StartTrainBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.商品.key());
    	record.setAction(LogActionEnum.车次启用.key());
    	record.setTargetId(param.getTrainId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
